﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.DesktopClient.Model;
using EzDesk.MVVMBase;
using EzDesk.DesktopClient.ViewContract;
using System.Windows;
using EzDesk.Generic.WPF;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.MVVMEnhance;

namespace EzDesk.DesktopClient.MessageModule
{
    public class FeedItemViewModel : ViewModelWithSingleContainer<IFeedItemView>
    {
        public FeedItemViewModel()
            : base()
        {

        }

        public FeedItemViewModel(Feed model)
            : base()
        {
            InitData(model);
            InitLocalEvent();
        }

        private void InitData(Feed model)
        {
            this.FeedModel = model;
        }


        private void InitLocalEvent()
        {
            this.View.OnNavigate += new EventHandler<Generic.GenericEventArg<string>>(OnNavigate);
            this.View.SetAsRead += new EventHandler(View_SetAsRead);
        }

        
        void OnNavigate(object sender, Generic.GenericEventArg<string> e)
        {
            FunctionNavigateEvent.Instance.Publish(new FunctionNavigateArgs { Url = e.Value });
        }

        void View_SetAsRead(object sender, EventArgs e)
        {
            this.IsRead = true;
            FeedStatusChangeEvent.Instance.Publish(this.FeedModel);
        }

        public Feed FeedModel
        {
            get;
            set;
        }

        public string ContentTitle
        {
            get { return this.FeedModel.TransformedTitle; }
            set
            {
                this.FeedModel.TransformedTitle= value;
            }

        }

        public string ContentBody
        {
            get { return this.FeedModel.TransformedBody; }
            set { this.FeedModel.TransformedBody = value; }
        }

        public DateTime LastUpdate
        {
            get { return this.FeedModel.LastChangeOn.Value; }
            set
            {
                this.FeedModel.LastChangeOn = value;
            }

        }

        public bool IsRead
        {
            get { return this.FeedModel.IsRead.Value; }
            set
            {
                this.FeedModel.IsRead = value;
                RaisePropertyChanged("IsRead");
            }
        }
    }
}
